﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using eCommerce.core.Helpers;
using eCommerce.db;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;
using System.Data;
using System.Configuration;
using eCommerce.core;

namespace eCommerce.web.Ajax.Product
{
    public partial class Filter : System.Web.UI.Page
    {
        public string name, parent_name, categoryName, Cat_url;
        public int pageSize;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var lstCategory      = Utils.GetIdFromRequestString(Utils.LstCategory, string.Empty);
                var lstPropertyValue = Utils.GetIdFromRequestString(Utils.LstPropertyValue, string.Empty);
                var orderBy          = Utils.GetIdFromRequestString(Utils.OrderBy, string.Empty);
                var pageIndex        = Utils.GetIdFromRequestString(Utils.PageIndex, Constant.PageIndex);
                pageSize         = Utils.GetIdFromRequestString(Utils.PageSize, Constant.PageSize);
                Cat_url = Utils.GetIdFromRequestString(Utils.Cat_url, string.Empty);

                ProductFilter(lstCategory, lstPropertyValue, orderBy, pageIndex, pageSize);
            }                
        }

        private void ProductFilter(string lstCategory, string lstPropertyValue, string orderBy, int pageIndex, int pageSize)
        {
            var totalRecords = 0;
            List<ProductEntity> lstProduct = null;
            // Nếu là Sản phẩm mới thì sẽ hiện isHot + isNew
            if (lstCategory == "0111")
                lstProduct = ProductBo.GetListLatest_Paging(((int)ProductStatus.BrandNew + (int)ProductStatus.Published), (int)pageIndex, (int)pageSize, ref totalRecords);
            else if (lstCategory == "0112") // Khuyến mại
                lstProduct = ProductBo.GetListDiscount_Paging((int)pageIndex, (int)pageSize, ref totalRecords);
            else
                lstProduct = ProductBo.GetListFilter(lstCategory, lstPropertyValue, orderBy, pageIndex, pageSize, ref totalRecords);

            rptProduct.DataSource = lstProduct;
            rptProduct.DataBind();


            string condition_paper = string.Format("'{0}', '{1}'", lstCategory, lstPropertyValue);
            string page_url = string.Format("javascript:getListFilterProduct({0},'{3}', {1}, {2})", condition_paper, "{0}", pageSize, orderBy);

            

            ltPager.Text = PagerHelper.PageLinks(pageIndex, pageSize, 100, Constant.PageStep, page_url);          
        }

        protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var ltCategory = (Literal)e.Item.FindControl("ltCategory");
                var categoryId = Utils.ToInt32(((HiddenField)e.Item.FindControl("hfCategoryId")).Value);
                Literal ltrHref = (Literal)e.Item.FindControl("ltrHref");
                Literal ltrHrefBelow = (Literal)e.Item.FindControl("ltrHrefBelow");
                Literal ltrHrefCode = (Literal)e.Item.FindControl("ltrHrefCode");
                Literal ltrIconNew = (Literal)e.Item.FindControl("ltrIconNew");
                Literal ltrPrice = (Literal)e.Item.FindControl("ltrPrice");

                var ltImage = ((Literal)e.Item.FindControl("ltImage"));

                var productId = Utils.ToInt32(((HiddenField)e.Item.FindControl("hfProductId")).Value);
                var name = Utils.UnsignAndDash(((HiddenField)e.Item.FindControl("hfName")).Value);
                var imagePath = ((HiddenField)e.Item.FindControl("hdImagePath")).Value;
                var status = ((HiddenField)e.Item.FindControl("hdStatus")).Value;
                var specialPrice = ((HiddenField)e.Item.FindControl("hdSpecialPrice")).Value;
                var unitPrice = ((HiddenField)e.Item.FindControl("hdUnitPrice")).Value;

                ltCategory.Text = string.Format("<em>Danh mục: <a href=\"{0}\">{1}</a></em>", "#", CategoryBo.GetCategoryName(categoryId));

                if (Cat_url == "san-pham-moi" || Cat_url == "khuyen-mai")
                {
                    var cate = CategoryBo.GetById(categoryId);
                    if (cate.Url.ToString().Contains("/"))
                    {
                        string[] arr = cate.Url.ToString().Split('/');
                        Cat_url = arr[0];
                    }
                }

                ltrHref.Text = ltrHrefCode.Text = "<a href=\" " + UrlHelper.ProductDetail(Cat_url, productId, name) + " \">";
                ltrHrefBelow.Text = "<a href=\" " + UrlHelper.ProductDetail(Cat_url, productId, name) + " \" class=\"compare tip\" title=\"\"></a>";

                string Img1 = ConfigurationManager.AppSettings["Thumbnail"] + string.Format("/Thumb.ashx?imgPath={0}&tid={1}&size=200", imagePath, (int)SettingType.Product);
                ltImage.Text = "<img src=\"" + Img1 + "\" />";

                if (status == ConfigurationManager.AppSettings["IsNew"] || status == "10")
                    ltrIconNew.Text =  "<p class=\"new\">New</p>";

                if (string.IsNullOrEmpty(specialPrice) || specialPrice == "0.0000")
                {
                    ltrPrice.Text = "<span class=\"price\">&nbsp;&nbsp; "+Utils.FormatPrice(Utils.ToDecimal(unitPrice))+"</span>";
                }
                else
                {
                    ltrPrice.Text = "<span class=\"price\"><small>"+Utils.FormatPrice(Utils.ToDecimal(specialPrice))+"</small>&nbsp;&nbsp; <small class='normal'>"+Utils.FormatPrice(Utils.ToDecimal(unitPrice))+"</small></span>";
                }
            }
        }
    }
}